package org.darkforest.common.services.impl;

import org.darkforest.common.services.CartService;
import org.darkforest.common.services.GoodsService;
import org.darkforest.common.services.SeckillService;
import org.darkforest.model.Cart;
import org.darkforest.model.Goods;
import org.darkforest.model.PMiaosha;
import org.darkforest.vo.MemberBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Libing on 2015/8/20.
 */

@Service
public class SeckillServiceImpl implements SeckillService {

    @Autowired
    private CartService cartService;

    @Autowired
    private GoodsService goodsService;
    /**
     * 创建秒杀放入购物车
     *
     * @param miaosha
     * @param userId
     * @return
     */
    @Override
    public Cart createMiaoshaCart(PMiaosha miaosha, Integer userId) {
        Cart cart = new Cart();
        Goods goods = goodsService.selectGoodsByPrimaryKey(miaosha.getGoodsId());
        cart.setGoodsSerial(goods.getGoodsSerial());
        cart.setStoreId(goods.getStoreId());
        cart.setBuyerId(userId.toString());
        cart.setGoodsNum((short) 1);
        cart.setGoodsId(goods.getGoodsId());
        cart.setMemberId(userId);
        cart.setGoodsImage(goods.getGoodsImage());
        cart.setGoodsName(goods.getGoodsName());
        cart.setGoodsPrice(miaosha.getMsPrice());
        cart.setStoreName(goods.getStoreName());
        cart.setBlId(0);
        cart.setSelected("1");
        cart.setActiveType("ms");
        cart.setActiveId(miaosha.getMsId());
        cartService.addCart(cart);
        return cart;
    }

    @Override
    public void cleanCartGoods(MemberBase memberBase) {
        cartService.clearCartByBuyerId(memberBase.getUserId().toString());
    }
}
